package com.codi.fcloud.ufx.hssale.query;

import com.codi.base.log.annotation.Loggable;
import com.codi.base.util.ExceptionUtil;
import com.codi.base.util.StringUtil;
import com.codi.bus.constant.GlobalConstant;
import com.codi.bus.exception.HundsunAPIException;
import com.codi.fcloud.ufx.UfxSDKFactory;
import com.codi.fcloud.ufx.constant.HSInputParameterNames;
import com.codi.fcloud.ufx.constant.UrlConstants;
import com.codi.trade.dto.HsIncomeQueryDto;
import com.codi.trade.dto.HsShareQueryDto;
import com.codi.trade.service.IncomeQueryService;
import com.codi.trade.service.ShareQueryService;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service("incomeQueryService")
public class IncomeQuery extends UfxSDKFactory implements IncomeQueryService {


    @Override
    @Loggable
    public Map<String, Object> query(HsIncomeQueryDto dto) {
        try {
            Map<String, Object>       request  = getRequestParams(dto);
            List<Map<String, Object>> response = getUfxSDK().query(getTarget(), request);

            // 如果requestNum是我们系统定义的最大值，则表明数据需要全部取出来
            getNextResponse(request, response, dto.getRequest_num());

            return appendLog(request, response);
        } catch (Exception exception) {
            ExceptionUtil.logError(logger, getTarget(), exception);
            throw new HundsunAPIException(ExceptionUtil.getMessage(exception));
        }
    }

    private Map<String, Object> getRequestParams(HsIncomeQueryDto dto) {

        String trustWay = apiValueMap.get(HSInputParameterNames.TRUST_WAY);

        Map<String, Object> request = new HashMap<>();

        request.put("trust_way", trustWay); // 交易委托方式
        request.put("request_num", getInteger(dto.getRequest_num().equals(GlobalConstant.MAX_REQUEST_NUM) ? 50:dto.getRequest_num(), 50)); //	N8	请求行数	Y	最大不能超过50；即最大返回记录条数不对查过50条；
        // 如果重新统计总记录数标志传0, 返回值 total_count值为空，传1就返回总记录数
        //	C1	重新统计总记录数标志	N	0-不重新统计；1-重新统计；
        request.put("reqry_recordsum_flag", getString(dto.getReqry_recordsum_flag(), "1"));
        //	N8	查询起始行号	N	比如总记录是100，每50条分页；那么需要查询2次；第1次查询请求行数传50，
        request.put("qry_beginrownum", getInteger(dto.getQry_beginrownum(), 1)); // 查询起始行号
        request.put("sort_direction", getString(dto.getSort_direction(), "1")); //	C1	返回排序方式	N
        //	C17	交易账号	N	交易账号、客户编号至少有一个为必填项
        request.put("trade_acco", getString(dto.getTrade_acco())); // 交易帐号
        request.put("ta_acco", getString(dto.getTa_acco())); // TA账号
        request.put("client_id", getString(dto.getClient_id())); // 客户编号
        request.put("fund_code", getString(dto.getFund_code())); // 基金代码
        request.put("share_type", getString(dto.getShare_type())); // 份额分类
        request.put("begin_date", getString(dto.getBegin_date()));
        request.put("end_date", getString(dto.getEnd_date()));
        //request.put("trade_mode", getString(dto.getTrade_mode()));
        return request;
    }


    @Override
    protected String getTarget() {
        return UrlConstants.PREFIX_SALE + UrlConstants.INCOME_QRY;
    }

}
